$(function () {
    $(".matching").click(function () {
        var storeName = $('#storeName').val();
        var shopkeeper = $('#shopkeeper').val();
        var phone = $('#phone').val();

        if (storeName && shopkeeper && phone) {
            $.ajax({
                url: ApiUrl + "/matchingStore",
                type: 'POST',
                data: {
                    storeName: storeName,
                    shopkeeper: shopkeeper,
                    phone: phone
                },
                dataType: 'JSON',
                success: function (result) {
                    if (result.code == 200) {
                        $('#c_storeId').val(result.data.storeId);
                        $('#c_address').val(result.data.address);
                        $('#c_storeName').val(storeName);
                        $('#c_shopkeeper').val(shopkeeper);
                        $('#c_phone').val(phone);
                        $('.form-match-store').hide();
                        $('.form-create-store').addClass('fadeInDown').show();
                        parent.layer.msg('匹配成功');
                    } else {
                        parent.layer.msg(result.message);
                    }
                }
            });
        } else {
            parent.layer.alert("资料不全！");
            return false;
        }
    });

    $('.btn-create-store').click(function () {
        var storeName = $('#c_storeName').val();
        var shopkeeper = $('#c_shopkeeper').val();
        var phone = $('#c_phone').val();
        var passwd = $('#c_passwd').val();
        var repasswd = $('#c_repasswd').val();

        if (passwd && repasswd && passwd === repasswd) {
            $.ajax({
                url: ApiUrl + "/admin",
                type: 'POST',
                data: {
                    storeId: $('#c_storeId').val(),
                    storeName: storeName,
                    shopkeeper: shopkeeper,
                    phone: phone,
                    password: passwd,
                    rePassword: repasswd,
                    real_name: shopkeeper,
                    tel: phone,
                    address: $('#c_address').val()
                },
                dataType: 'JSON',
                success: function (result) {
                    if (result.code == 200) {
                        syncData(function(){
                            parent.layer.msg('创建成功，请登录');
                            setTimeout(function () {
                                window.location.replace('login.html');
                            }, 2000);
                        });
                    } else {
                        parent.layer.msg(result.message);
                    }
                }
            });
        } else {
            parent.layer.alert("资料不全或者密码不一致！");
        }
    });

    function syncData(callback) {
        syncPromise('initMember').then(function (respMember) {
            parent.layer.msg(!!respMember ? '初始化会员完成！' : '初始化会员失败！');
            if (!!respMember) {
                return syncPromise('initCategory');
            } else {
                return $.Deferred().reject();
            }
        }).then(function (respCategory) {
            parent.layer.msg(!!respCategory ? '初始化商品分类完成！' : '初始化商品分类失败！');
            if (!!respCategory) {
                return syncPromise('initGoods');
            } else {
                return $.Deferred().reject();
            }
        }).then(function (respGoods) {
            parent.layer.msg(!!respGoods ? '初始化商品完成！' : '初始化商品失败！');
            if (!!respGoods && callback) {
                callback();
            }
        });
    }

    function syncPromise(url) {
        var def = $.Deferred();
        $.ajax({
            url: ApiUrl + '/' + url,
            type: 'GET',
            success: function (result) {
                if (result.code == 200) {
                    def.resolve(true);
                } else {
                    def.reject(false);
                }
            }
        });
        return def;
    }
});